package io.gitee.xyuesheng.day02;

import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;

public class DemoArray {
    @Test
    public void test1() {
//        String s = "abc";
//        String s = new String("abc");
        System.out.println();

        // 数组是对象类型
        int[] array = new int[10];

        array[0] = 1;
        array[1] = 2;

        int[] intArray = {1, 2, 3};    // 快速初始化

        int[] intArray2 = new int[3];
        intArray2[0] = 1;
        intArray2[1] = 2;
        intArray2[2] = 3;

        char[] charArray = {'A', 'B', 'C'};
        String[] stringArray = {"Abc", "DEF"};

        // 对象有方法、属性
        System.out.println(charArray.length);

        // stringArray[2] = "GHI";     // 不能扩容，数组越界
    }

    @Test
    public void testArrayIteration() {
        int[] array = {1, 2, 3, 4};

        // 需要使用数组下标时使用
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }

        for (int element : array) {
            System.out.println(element);
        }

        char[] charArray = {'A', 'D', 'G'};

        // for each 循环
        for (char item : charArray) {
            System.out.println(item);
        }
    }

    @Test
    public void testPrintArray() {
        char[] array = {'A', 'G', 'E'};

        System.out.println(Arrays.toString(array));
    }

    @Test
    public void testSearch() {
        int[] array = {2, 8, 5, 15, 99, 55, 33};

        Arrays.sort(array);

        System.out.println(Arrays.toString(array));
    }

    @Test
    public void testBinarySearch() {
        int[] array = {2, 5, 8, 15, 33, 55, 99};

        // 断言
        Assert.assertEquals(-1, binarySearch(array, 3));
        Assert.assertEquals(0, binarySearch(array, 2));
        Assert.assertEquals(2, binarySearch(array, 8));
    }

    public int binarySearch(int[] array, int target) {
        int low = 0, high = array.length - 1;

        while (low <= high) {
            int middle = (low + high) / 2;

            if (target < array[middle]) {
                high = middle - 1;
            } else if (target > array[middle]) {
                low = middle + 1;
            } else {
                System.out.println("找到位置：" + middle);
                return middle;
                // break;
            }
        }

        System.out.println("没找到。。。");

        return -1;
    }

    @Test
    public void testArray2D() {
        int[][] scores = {
                {50, 55, 53},
                {121, 101, 98},
                {60, 50, 40},
        };

        System.out.println("张三的政治：" + scores[2][0]);

        scores[2][0] = 61;

        System.out.println(Arrays.toString(scores[0]));

        System.out.println(scores.length);
        System.out.println(scores[0].length);

        System.out.println();
    }

    @Test
    public void testBrick() {
        int[][] brick = {
//                {1, 1, 1},
//                {0, 1, 0},
//                {0, 0, 0}
                {1, 1, 0},
                {0, 1, 0},
                {0, 1, 0}
        };

//        int[][] brick2 = {
//                {0, 0, 1},            // i = 0
//                {0, 1, 1},
//                {0, 0, 1},
//        };

        int[][] brick2 = new int[3][3];

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                brick2[i][j] = brick[2 - j][i];
            }
        }

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(brick2[i][j] + ",");
            }

            System.out.println();
        }
    }

    @Test
    public void testChar() {
        int i = 3;
        char c = 'A';

        System.out.println((int) c);
        System.out.println(i + c);
    }
}
